﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConvergensDominoConnector;

namespace CDCOSDH
{
    public partial class ActiveIncomingDocsByOffice : Form
    {
        private string sdhName;
        private string viewName;

        public List<string> selectedRow = new List<string>(10);

        public ActiveIncomingDocsByOffice(string sdhName, string viewName)
        {
            InitializeComponent();

            this.sdhName = sdhName;
            this.viewName = viewName;
            this.Text = "Kontorenes indgående i restance i " + this.sdhName + " SDH";

            this.dTActiveIncomingDocsByOffice.Select();
        }

        private void ActiveIncomingDocsByOffice_Load(object sender, EventArgs e)
        {
            try
            {
                DataView dataView = new DataView(DominoConnector.Default.GetAllActiveCases(this.viewName, this.sdhName));
                this.dTActiveIncomingDocsByOffice.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
                this.dTActiveIncomingDocsByOffice.RowHeadersVisible = false;
                this.dTActiveIncomingDocsByOffice.DataSource = dataView;

                //DataTable dataTable = DominoConnector.Default.GetAllActiveCases(this.viewName, this.sdhName);
                //this.dataTableAllActiveCases.DataSource = dataTable;
                this.dTActiveIncomingDocsByOffice.Columns["UNID"].Visible = false;
                this.dTActiveIncomingDocsByOffice.Columns[0].Visible = false;
                this.dTActiveIncomingDocsByOffice.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
                this.dTActiveIncomingDocsByOffice.Columns["Svar-løbenummer"].HeaderText = "Svarløbenummer";
                this.dTActiveIncomingDocsByOffice.Sort(this.dTActiveIncomingDocsByOffice.Columns["Svar-løbenummer"], ListSortDirection.Ascending);
                this.dTActiveIncomingDocsByOffice.Columns[0].Width = 98;
                this.dTActiveIncomingDocsByOffice.Columns[1].Width = 550;
                this.dTActiveIncomingDocsByOffice.Columns[2].Width = 83;
                //this.dTActiveIncomingDocsByOffice.AutoResizeColumns();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //DataTable dataTable = DominoConnector.Default.GetActiveIncommingDocsByOffice(this.viewName, this.sdhName);
            //this.dTActiveIncomingDocsByOffice.DataSource = dataTable;

            //this.lvActiveIncomingDocsByOffice.SuspendUpdate();

            //this.lvActiveIncomingDocsByOffice.Groups.Clear();

            //for (int a = 0; a < dataTable.Columns.Count; a++)
            //{
            //    LidorSystems.IntegralUI.Lists.ListViewColumn columnsName = new LidorSystems.IntegralUI.Lists.ListViewColumn(dataTable.Columns[a].ColumnName);
            //    this.lvActiveIncomingDocsByOffice.Columns.Add(columnsName);
            //}

            //LidorSystems.IntegralUI.Lists.ListViewItem item = null;
            //LidorSystems.IntegralUI.Lists.ListViewSubItem subItem = null;
            //LidorSystems.IntegralUI.Lists.ListViewGroup group = null;

            //foreach (DataRow dr in dataTable.Rows)
            //{
            //    item = new LidorSystems.IntegralUI.Lists.ListViewItem();

            //    foreach (DataColumn dc in dataTable.Columns)
            //    {
            //        subItem = new LidorSystems.IntegralUI.Lists.ListViewSubItem(dr[dc].ToString());
            //        if (subItem.Value != null)
            //        {
            //            item.SubItems.Add(subItem);
            //        }
            //    }
            //    if (item != null)
            //    {
            //        this.lvActiveIncomingDocsByOffice.Items.Add(item);
            //    }
            //}

            //var distinctGroups = (from Rows in dataTable.AsEnumerable()
            //                      select Rows[0]).Distinct().ToList();

            //for (int k = 0; k < distinctGroups.Count; k++)
            //{
            //    if (distinctGroups[k].ToString().Equals(string.Empty))
            //    {
            //        distinctGroups[k] = "Ikke kategorisereret";
            //    }

            //    group = new LidorSystems.IntegralUI.Lists.ListViewGroup(distinctGroups[k].ToString());
            //    this.lvActiveIncomingDocsByOffice.Groups.Add(group);
            //}

            // Assign items to groups depending on their index
            //int groupIndex = 0;
            //List<string> addedGroup = new List<string>(500);
            //if (addedGroup.Count > 0)
            //{
            //    addedGroup.Add(this.lvActiveIncomingDocsByOffice.Items[0].SubItems[0].Text);

            //    for (int i = 0; i < this.lvActiveIncomingDocsByOffice.Items.Count; i++)
            //    {
            //        if (addedGroup.Exists(element => element.Equals(this.lvActiveIncomingDocsByOffice.Items[i].SubItems[0].Text)))
            //        {
            //            foreach (DataColumn dc in dataTable.Columns)
            //            {
            //                if (dc.ColumnName == "Column1")
            //                {
            //                    this.lvActiveIncomingDocsByOffice.Items[i].SubItems[0].Text = string.Empty;
            //                    continue;
            //                }
            //                else
            //                {
            //                    this.lvActiveIncomingDocsByOffice.Groups[groupIndex].Items.Add(this.lvActiveIncomingDocsByOffice.Items[i]);
            //                }

            //            }
            //            continue;
            //        }
            //        else
            //        {
            //            groupIndex++;

            //            addedGroup.Add(this.lvActiveIncomingDocsByOffice.Items[i].SubItems[0].Text);

            //            foreach (DataColumn dc in dataTable.Columns)
            //            {
            //                if (dc.ColumnName == "Column1")
            //                {
            //                    this.lvActiveIncomingDocsByOffice.Items[i].SubItems[0].Text = string.Empty;
            //                    continue;
            //                }
            //                else
            //                {
            //                    this.lvActiveIncomingDocsByOffice.Groups[groupIndex].Items.Add(this.lvActiveIncomingDocsByOffice.Items[i]);
            //                }
            //            }
            //        }
            //    }

            //    addedGroup.Clear();
            //    addedGroup = null;
            //}

            //this.lvActiveIncomingDocsByOffice.ShowGroups = true;

            //this.lvActiveIncomingDocsByOffice.Columns[5].Visible = false;

            //this.lvActiveIncomingDocsByOffice.ResumeUpdate();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            List<string> selectedRow = new List<string>(10);

            foreach (DataGridViewRow row in this.dTActiveIncomingDocsByOffice.SelectedRows)
            {
                selectedRow.Add(row.Cells[1].Value.ToString());
                selectedRow.Add(row.Cells[2].Value.ToString());
                selectedRow.Add(row.Cells[3].Value.ToString());
            }

            this.selectedRow.AddRange(selectedRow);
            selectedRow = null;

            this.DialogResult = DialogResult.OK;
        }

        private void btnCnl_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void dTActiveIncomingDocsByOffice_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.dTActiveIncomingDocsByOffice.SelectedRows.Count == 0)
            {
                return;
            }
            if (e.KeyCode == Keys.Enter)
            {
                List<string> selectedRow = new List<string>(10);
                foreach (DataGridViewRow row in this.dTActiveIncomingDocsByOffice.SelectedRows)
                {
                    selectedRow.Add(row.Cells[1].Value.ToString());
                    selectedRow.Add(row.Cells[2].Value.ToString());
                    selectedRow.Add(row.Cells[3].Value.ToString());
                }

                this.selectedRow.AddRange(selectedRow);
                selectedRow = null;
                this.DialogResult = DialogResult.OK;
            }
        }

        private void dTActiveIncomingDocsByOffice_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            List<string> selectedRow = new List<string>(10);

            foreach (DataGridViewRow row in this.dTActiveIncomingDocsByOffice.SelectedRows)
            {
                selectedRow.Add(row.Cells[1].Value.ToString());
                selectedRow.Add(row.Cells[2].Value.ToString());
                selectedRow.Add(row.Cells[3].Value.ToString());
            }

            this.selectedRow.AddRange(selectedRow);
            selectedRow = null;

            this.DialogResult = DialogResult.OK;
        }
    }
}
